using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ShopForWeb.Services;
using ShopForWeb.UI.Models;
using Microsoft.Extensions.Configuration.UserSecrets;


using ShopForWeb.UI.Utils.Extensions;
using Azure.Identity;
using ShopForWeb.UI.Utils.Filters;

namespace ShopForWeb.UI.Controllers
{
    
    [Route("[controller]")]
    [UserInfo]
    public class IdentityController : Controller
    {
        private readonly ILogger<IdentityController> _logger;
        private UserService _userService;

        public IdentityController(ILogger<IdentityController> logger,UserService userService)
        {
            _logger = logger;
            _userService=userService;
        }

        public IActionResult Index()
        {
            return View();
        }
[HttpPost("login")]
  public async Task<IActionResult> Login(string phone,string password)
   {
    var user=await _userService.GetByLoginAsync(phone,password);
    if(user!=null)
    {
        this.SetUser(new Model.Basic.User()
        {
            UserId=user.UserId,
            UserName=user.UserName,
            Phone=user.Phone
        }
        );
        return Json(new ResponseViewModel()
        {
            Code=200,
            Message=$"欢迎{user.UserName}的到来"

        }
        );
    }
    else
    return Json(new ResponseViewModel()
        {
            Code=500,
            Message=$"错误的电话或密码,请重新输入"
        }
        );
    }
     [Route("logout")]
     public IActionResult Logout()
 {
      var cookies=this.HttpContext.Response.Cookies;
       cookies.Delete("key-user");
       return Json(new ResponseViewModel()
{
    Code=200,
    Message="您已安全退出"
 });
 }
 [HttpGet("regsiter")]
 public IActionResult Regsiter()
 {
    return View();
 }

}}